package com.kuang.demo.service.wx;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuang.demo.bean.wx.Collect;
import com.kuang.demo.bean.wx.CollectAddordeleteBean;
import com.kuang.demo.bean.wx.CollectExample;
import com.kuang.demo.bean.wx.CollectListVo;
import com.kuang.demo.mapper.wx.CollectMapper3;
import com.kuang.demo.mapper.wx.CollectMapper2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

/**
 * @author ACG
 * @date 2020-11-05 20:50
 */
@Service
public class CollectServiceImpl implements CollectService{
@Autowired
CollectMapper3 collectMapper;

@Autowired
CollectMapper2 collectMapper1;
    @Override
    public String addordeleteControl(CollectAddordeleteBean collectAddordeleteBean) {
     Integer UserId  =  collectMapper.selectCollectMakeSureAddOrDelete(collectAddordeleteBean.getValueId());
     if (UserId!=null) {
         collectMapper.deleteCollect(collectAddordeleteBean);
         return "delete";
     }
     collectMapper.insertCollect(collectAddordeleteBean);
        return "add";
    }

    @Override
    public CollectListVo listControl(Integer type, Integer page, Integer size) {
        CollectListVo collectListVo = new CollectListVo();
        List<CollectListVo.CollectListBean> list = collectMapper.selectGoodsAndCollectId();
        PageHelper.startPage(page,size);
        CollectExample collectExample = new CollectExample();
//        //设置order by 后面的内容
//        userExample.setOrderByClause(sort +" "+order); //add_time desc
        List<Collect> collects = collectMapper1.selectByExample(collectExample);
        //可以在查询完成后获得pageInfo → 根据查询结果获得
        PageInfo<Collect> collectPageInfo = new PageInfo<>(collects);
        int total = (int) collectPageInfo.getTotal();//根据sql查询条件执行了一个select count(*)
        collectListVo.setCollectList(list);
        collectListVo.setTotalPages(total);
        return collectListVo;
    }
}
